package com.cc.flyingfish;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;

/**
 * @author CHENCHEN
 * @title
 * @createTime 2023-03-04 13:57
 */
public class MyBatisGenerator {

    //数据库名称
    static String databaseName = "points";
    //数据库用户名
    static String username = "root";
    //数据库密码
    static String passworld = "1q2w3e4r_DF";
    //生成文件存放包路径
    static String parent = "com.cc.flyingfish";
    //表前缀
    static String[] tablePrefix;
    //表后缀
    static String[] tableSuffix;
    //字段前缀
    static String[] fieldPrefix;
    //字段后缀
    static String[] fieldSuffix;
    //需要生成的表名
    String[] tables;

    @Before
    public void initTables(){
        tables = new String[] {
                "points"
        };
        tablePrefix = new String[] {};
        tableSuffix = new String[] {};
        fieldPrefix = new String[] {};
        fieldSuffix = new String[] {};
    }

    @Test
    public void generator(){
        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/" + databaseName + "?&useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai",
                username, passworld)
                .globalConfig(builder -> {
                    builder.outputDir(System.getProperty("user.dir") + "/src/main/java")
                            .disableOpenDir()
                            .author("CHENCHEN")
                            .enableSwagger()
                            .dateType(DateType.TIME_PACK)
                            .commentDate("yyyy-MM-dd")
                            .build();
                }).packageConfig(builder -> {
                    builder.parent(parent)
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .xml("mapper.xml")
                            .controller("controller")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + "/src/main/resources/mapping/"))
                            .build();
        }).strategyConfig(builder -> {
            builder.enableCapitalMode()
                    .enableSkipView()
                    .disableSqlFilter()
                    .addInclude(tables)
                    .addTablePrefix(tablePrefix)
                    .addTableSuffix(tablePrefix)
                    .addFieldPrefix(fieldPrefix)
                    .addFieldSuffix(fieldSuffix)
                    .build();
        }).strategyConfig(builder -> {
            builder.entityBuilder()
                    .disableSerialVersionUID()
                    .enableChainModel()
                    .enableLombok()
                    //.enableRemoveIsPrefix()
                    .enableTableFieldAnnotation()
                    //.enableActiveRecord()
                    .versionColumnName("version")
                    .versionPropertyName("version")
                    .logicDeleteColumnName("deleted")
                    .logicDeletePropertyName("deleteFlag")
                    //.addSuperEntityColumns("id", "created_by", "created_time", "updated_by", "updated_time")
                    .idType(IdType.ASSIGN_ID)
                    .formatFileName("%sEntity")
                    .build();
        }).strategyConfig(builder -> {
            builder.serviceBuilder()
                    .formatServiceFileName("%sService")
                    .formatServiceImplFileName("%sServiceImp")
                    .build();
        }).strategyConfig(builder -> {
            builder.mapperBuilder()
                    .superClass(BaseMapper.class)
                    .enableMapperAnnotation()
                    .enableBaseResultMap()
                    .enableBaseColumnList()
                    .formatMapperFileName("%sMapper")
                    .formatXmlFileName("%sMapper")
                    .build();
        }).strategyConfig(builder -> {
            builder.controllerBuilder()
                    .enableRestStyle()
                    .formatFileName("%sController")
                    .build();
        }).execute();

    }

}
